//---------------------------
// Includes
//---------------------------
#include "Level.h"
#include "LevelElement.h"
#include "InputState.h"
#include "RenderContext.h"
#include "PhysX.h"
#include <algorithm>
//---------------------------
// Constructor & Destructor
//---------------------------
Level::Level(ID3D10Device *pD3DDevice, PhysX* physx, POINT windowsize)
:m_pD3DDevice(pD3DDevice)
,m_pPhysX(physx)
,m_WindowSize(windowsize)
{
	// nothing to create
}

Level::~Level()
{
	//delete all GraphElements
	for(unsigned int i=0;i<m_Nodes.size();++i)
	{
		SafeDelete (m_Nodes.at(i));
	}
	//SafeDelete(m_pPhysX);
}

void Level::AddChild(LevelElement *pNewGraphElement)
{
	pNewGraphElement->SetParent(this);
	m_Nodes.push_back(pNewGraphElement);
}

void Level::RemoveChild(LevelElement *childElement){
	vector<LevelElement*>::const_iterator foundElement;
	
	foundElement = std::find(m_Nodes.begin(),m_Nodes.end(),childElement);
	if ( foundElement != m_Nodes.end())
		m_Nodes.erase(foundElement);
}

void Level::Initialize(ContentManager *pContentManager)
{
	//call Initialize on every child
	std::vector<LevelElement*>::iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->Initialize(pContentManager);
	}
}

void Level::Tick(const InputState & refInputState)
{
	//call Tick on every child
	std::vector<LevelElement*>::iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->Tick(refInputState);
	}
}

void Level::Draw(const RenderContext* pRenderContext)
{
	//call Draw on every child
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->Draw(pRenderContext);
	}
}
void Level::DrawRefraction(const RenderContext* pRenderContext, D3DXPLANE refractionplane)
{
	//call Draw on every child
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->DrawRefraction(pRenderContext, refractionplane);
	}
}
void Level::DrawReflection(const RenderContext* pRenderContext, D3DXMATRIX reflectionviewmatrix, D3DXPLANE reflectionplane)
{
	//call Draw on every child
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->DrawReflection(pRenderContext, reflectionviewmatrix, reflectionplane);
	}
}
void Level::DrawShadowMap(const RenderContext* pRenderContext)
{
	//call Draw on every child
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->DrawShadowMap(pRenderContext);
	}
}
void Level::OnResize(int clientWidth, int clientHeight)
{
	//call Draw on every child
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->OnResize(clientWidth, clientHeight);
	}
}
void Level::SetShadowDepthMap(ID3D10ShaderResourceView* shadowmap)
{
	std::vector<LevelElement*>::const_iterator  it;
	for(it=m_Nodes.begin();it<m_Nodes.end();++it)
	{
		(*it)->SetShadowMap(shadowmap);
	}
}
NxScene *Level::GetPhysicsScene() const 
{
	return m_pPhysX->GetScene();
}
